<?php
$ROOT_DIR_PATH = '../';
require_once($ROOT_DIR_PATH . 'includes/includes.php');
require_once($ROOT_DIR_PATH . 'classes/class.banner.inc.php');

$ADMIN_PAGE_TITLE = 'Banner Page';
$bannerImagePath = $ROOT_DIR_PATH.'upload/images/banner/';
$listingUrl = $ADMIN_BASE_HREF . "bannerlisting.php";

$loginUrl = $ADMIN_BASE_HREF . "login.php";
if (!$GENOBJ->isLoggedIn()) {
    $GENOBJ->redirect($loginUrl);
}

$GENOBJ->clearSessionErrorMessages();
$mode = 'insert';
$bannerObj = new banner();
if (isset($_GET['id'])) {
    if ((trim($_GET['id']) == '')) {
	$GENOBJ->redirect($listingUrl);
	exit;
    }
    $bannerObj->setid($_GET['id']);
    $mode = 'edit';

    if (isset($_GET['mode'])) {
	if ((trim($_GET['mode']) == 'delete')) {
		$deleteId = $_GET['id'];
		$deleteId = $GENOBJ->filter($deleteId, Array("utfencode", "TrimElement", "htmlEntity"));
	    $bannerObj->select($deleteId);
	    $bannerObj->delete($deleteId);
	    $bannerObj->deleteImage($bannerObj->getname());
	    $_SESSION['error']['banner-updated'] = 'Banner Image deleted successfully';
	    $GENOBJ->redirect($listingUrl);
	    exit;
	}
    }

}

if (isset($_POST['id'])) {
    if (trim($_POST['id']) != '') {
	$bannerObj->setid($_POST['id']);
	$mode = 'edit';
    }
}

if ($mode == 'edit') {
    $id = $bannerObj->getid();
    $bannerData = $bannerObj->select($id);
    $filterArray = Array("utfdecode"); //Utf change
    $bannerData = $GENOBJ->filter($bannerData, $filterArray); //Utf change
    $bannerObj->setAll($bannerData);
}
$errorMessageArray = Array();
if ($_POST) {
    $filterElements = Array();
    $filterArray = Array("utfencode", "TrimElement", "htmlEntity");  //Utf change
    $filterElements['id'] = $_POST['id'];
    $filterElements['title'] = $_POST['title'];	$filterElements['link'] = $_POST['link'];	$filterElements['description'] = $_POST['description'];
    $filterElements = $GENOBJ->filter($filterElements, $filterArray);
    $bannerObj->setAll($filterElements);

    if ($GENOBJ->IsEmpty($bannerObj->gettitle())) {
	$errorMessageArray[] = 'Please enter title.';
    }		if ($GENOBJ->IsEmpty($bannerObj->getdescription())) {        $errorMessageArray[] = 'Please enter description.';    }	if (!$GENOBJ->IsEmpty($bannerObj->getlink())) {		if (!$GENOBJ->ValidUrl($bannerObj->getlink())) {			$errorMessageArray[] = 'Link is not valid url';		}    }
    $imageAllowedFormat = $GENOBJ->allowedImageFormats();
    if (trim($_POST['id']) != '') {
	$mode = 'edit';
	if ($bannerObj->getname() == "") {
	    if ($_FILES['image']['name'] == '') {
		$errorMessageArray[] = 'Please upload image.';
	    } else {
		$responseText = $GENOBJ->image_validate($imageAllowedFormat);
		if ($responseText == 'IMPROPER_FORMAT') {
		    $errorMessageArray[] = 'Please upload image with valid file format.';
		}
	    }
	}
    } else {
	$mode = 'insert';
	if ($_FILES['image']['name'] == '') {
	    $errorMessageArray[] = 'Please upload image.';
	} else {
	    $responseText = $GENOBJ->image_validate($imageAllowedFormat);
	    if ($responseText == 'IMPROPER_FORMAT') {
		$errorMessageArray[] = 'Please upload image with valid file format.';
	    }
	}
    }

    if (count($errorMessageArray) <= 0) {
	if ($mode == 'insert') {
		$bannerObj->setaddeddate(date('Y-m-d h:i:s'));
		$bannerObj->setupdateddate(date('Y-m-d h:i:s'));
	    $insertid = $bannerObj->insert();
	    if ($insertid > 0) {
		if ($_FILES['image']['name'] != '') {
		    $fileArray = Array(
			'image' => 'image',
			'id' => $insertid,
			'target_path' => $bannerImagePath,
			'imageObj' => $bannerObj,
			'type' => 'banner'
		    );
		    $imageName = $GENOBJ->moveImageObject($fileArray);
		}
		$_SESSION['error']['banner-inserted'] = 'Banner Image Added successfully';
		$GENOBJ->redirect($listingUrl);
	    } else {
		$_SESSION['error']['banner-error'] = 'Error in insertion . Please try again!';
		$GENOBJ->redirect($listingUrl);
	    }
	} else {
		$bannerObj->setupdateddate(date('Y-m-d h:i:s'));
	    $bannerObj->update($id);

	    if ($_FILES['image']['name'] != '') {
		$fileArray = Array(
		    'image' => 'image',
		    'id' => $id,
		    'target_path' => $bannerImagePath,
		    'imageObj' => $bannerObj,
		    'type' => 'banner'
		);
		$GENOBJ->moveImageObject($fileArray);
	    }

	    $_SESSION['error']['banner-updated'] = 'Banner Image updated successfully';
	    $GENOBJ->redirect($listingUrl);
	}
    }
}
?>
<?php include('templates/header.php'); ?>
<body>
    <div id="banner"></div>
    <div id="ncleangrey-container">
        <?php include('templates/navigation.php'); ?>
        <div class="breadcrumbs">
            <div id="nav-icons_all">
                <ul id="nav-icons">
                    <li class="logout-icon"><a  title="Logout"  href="logout.php">Logout</a></li>
                </ul>
            </div>
            <p class="breadcrumbs">
                <a title="Home" href="dashboard.php">Home</a> &#187; <a title="banner" href="bannerlisting.php">Banner</a> &#187; <?php if ($mode == 'edit') { ?>Edit<?php } else { ?>Add<?php } ?> Banner
            </p>
        </div>
        <div class="hstippled">&nbsp;</div>
        <div id="MainContent">
            <div>
                <div class="pageerrorcontainer-validator">
                    <div class="error-iterator"></div>
                </div>
            </div>
            <?php if (count($errorMessageArray) > 0): ?>
                <div class="pageerrorcontainer">
                <?php foreach ($errorMessageArray as $erroValue): ?>
                    <div class="pageoverflow">
                        <img src="images/icons/system/stop.gif" class="systemicon" alt="Error" title="Error" />
                        <label class="errorlabel"><?php echo $erroValue; ?></label>
                    </div>
                <?php endforeach; ?>
                </div>
            <?php endif; ?>
                    <div class="clearb">&nbsp;</div>
                    <div id="Edit_Content_Result"></div>
                    <div class="pagecontainer pageoverflow">
                        <div class="pageheader">
                    <?php if ($mode == 'edit') { ?>Edit<?php } else { ?>Add<?php } ?> Banner
                    <div class="field-required">* Fields are required </div>
                </div>
                <div style="clear: both;"></div>
                <form method="post" enctype="multipart/form-data" action="banner.php" name="bannerfrm" id="bannerfrm">
                    <input type="hidden" id="id" value="<?php echo $bannerObj->getid(); ?>" name="id">
		    
                    <div id="page_content">
                        <div class="pageoverflow">
                            <div class="pagetext">Title<span class="error-star">*</span>:</div>
                            <div class="pageinput">
                                <input type="text" name="title" id="title" value="<?php echo $bannerObj->gettitle(); ?>" size="60"/>
                            </div>
                        </div>																		<div class="pageoverflow">							<div class="pagetext">Description<span class="error-star">*</span>:</div>							<div class="pageinput">								<textarea id="description" name="description" cols="50" rows="10" ><?php echo $bannerObj->getdescription();?></textarea>							</div>						</div>												<div class="pageoverflow">                                   <div class="pagetext">Link :</div>                                   <div class="pageinput">                                        <input type="text" name="link" id="link" value="<?php echo $bannerObj->getlink(); ?>" size="60"/>                                   </div>						</div>												
                        
						<?php if($bannerObj->getname() == ''):?>
						<br/>
						<br/>
						<div id="upload-image-container" class="pageoverflow upload-image-container">
							<p class="pagetext">Image:</p>
								<p class="pageinput">
									<input id="image_id" name="image"  class="image_class" type="file" />
							   </p>
						</div>
						<?php endif;?>
                        <div class="pageoverflow">
						<?php if($bannerObj->getname() != ''):?>
							<br/>
							<br/>
							<div class="pageoverflow image-container">
								<div class="floatleft pageinput">
									<a style="text-decoration: none;" class="lightbox" href="<?php echo $ROOT_DIR_PATH;?>upload/images/banner/<?php echo $bannerObj->getname();?>">
								<?php
									$imageName = $bannerObj->getname();
									$src_path = '../upload/images/banner/thumb_'.$imageName;
								?>
										<img src="<?php echo $src_path; ?>" />&nbsp;
								   </a>
								 </div>
							   <div class="floatleft">
								  <input type="button" value="Edit Banner Image" name="<?php echo $bannerObj->getname();?>" class="image-delete" id="<?php echo $bannerObj->getid()?>"/>
							   </div>
							</div>
						<?php endif;?>
						</div>
							  
							  
							  
							  
							  
                        <div class="pageoverflow">
                            <p class="pagetext">&nbsp;</p>
                            <p class="pageinput">
                                <input type="submit" name="submitbutton" value="Submit"  title="Save changes" />
                                <input type="reset" name="apply" value="Reset" title="Save changes and continue to edit" />
                            </p>
                        </div>
                        <div style="clear: both;">&nbsp;</div>
                    </div>
                </form>
            </div>
            <p class="pageback">
                <a class="pageback" href="bannerlisting.php">&#171; Back to Listing</a>
            </p>
            <div class="clearb"></div>
        </div>
    </div>
<?php include('templates/footer.php'); ?>
</body>
</html>